<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2018/6/23
 * Time: 10:46
 */

namespace app\common\model;


use think\Model;

class Comments extends Model
{
    //根据ID获取每页的数据
    public function getCommentsById($id,$page,$pageSize = 10){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }

        $where = array(
            'c.status'=>1,
            'c.course_id'=>$id
        );

        $offset = ($page - 1) * $pageSize;
        return $this->where($where)->limit($offset,$pageSize)->alias("c")->join("xm_users u","c.user_id=u.id")->field("c.content,c.comment_time,c.is_select,c.is_top,c.reply,c.reply_time,u.name,u.image_url")->select();
    }

    //根据ID获取评论总数
    public function getCountById($id){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }

        $where = array(
            'status' => 1,
            'course_id' => $id,
        );

        return $this->where($where)->count();
    }

    //添加评论
    public function getCommentAdd($data){
        if(!$data && !is_array($data)){
            exception("传递的数据不合法");
        }
        $this->save($data);
        return $this->id;
    }

    //根据用户ID查询
    public function getIsEvaluateById($id){
        if(!$id && !is_numeric($id)){
            return false;
        }

        $where = array(
            'user_id' => $id,
            'status' => 1,
        );

        return $this->where($where)->select();
    }
}